import {
  USER_LOGIN,
  USER_AUTH,
  DEL_CARD,
  SET_PRIMARY_CARD,
  SAVE_BANK_CARDS,
  SAVE_USER_INFO,
  SAVE_LUCKY_PRIZES,
  CHANGE_LIST_PAGE_DATA
} from './mutation-types'
import Util from '@/utils/Util'

export default {
  [USER_LOGIN] (state) {
    state.authorized = true
  },
  [USER_AUTH] (state, idCardNo) {
    state.user.idAuthed = true
    state.user.idCardNo = idCardNo
  },
  [SAVE_USER_INFO] (state, user) {
    state.user = user
  },
  [SAVE_BANK_CARDS] (state, cards) {
    state.bankCards = Util.prepareBankCards(cards)
  },
  [CHANGE_LIST_PAGE_DATA] (state, pageData) {
    state.pageData = pageData
  },
  [DEL_CARD] (state, id) {
    let cards = state.bankCards
    if (cards.length <= 0) {
      return
    }

    for (let i = 0, l = cards.length; i < l; i++) {
      if (id === cards[i].id) {
        cards.splice(i, 1)
      }
    }
    state.bankCards = cards
  },
  [SET_PRIMARY_CARD] (state, id, cardType) {
    let cards = state.bankCards
    if (cards.length <= 0) {
      return
    }

    for (let i = 0, l = cards.length; i < l; i++) {
      let t = parseInt(cards[i]['type'])
      if (id === cards[i].id && t === cardType) {
        cards[i]['primary'] = 1
      } else if (t === cardType) {
        cards[i]['primary'] = 0
      }
    }
    state.bankCards = cards
  },
  [SAVE_LUCKY_PRIZES] (state, prizes) {
    state.prizes = prizes
  }
}
